<?php

RegisterPlugin('acmeContract', 'ActivePlugin_acmeContract');

function ActivePlugin_acmeContract()
{
    global $zbp;
    $zbp->LoadLanguage('theme', 'acmeContract');
    $zbp->lang['msg']['first_button'] = '首页';
    $zbp->lang['msg']['prev_button'] = '上一页';
    $zbp->lang['msg']['next_button'] = '下一页';
    $zbp->lang['msg']['last_button'] = '尾页';
    $zbp->lang['msg']['sidebar'] = '首页侧栏';
    $zbp->lang['msg']['sidebar2'] = '分类列表侧栏';
    $zbp->lang['msg']['sidebar3'] = '文章页侧栏';
    $zbp->lang['msg']['sidebar4'] = '单页侧栏';
    $zbp->lang['msg']['sidebar5'] = '搜索列表侧栏';
    $zbp->lang['msg']['sidebar6'] = '其他列表侧栏';
    Add_Filter_Plugin('Filter_Plugin_ViewSearch_Begin', 'acmeContract_ViewSearch_Begin');
    // 自定义模块
    if (!isset($zbp->modulesbyfilename['topArticle_module'])) { //检查系统中是否已有该模块，没有则创建
        $t = new Module(); //实例化模块
        $t->Name = "热门文章"; //模块名称
        $t->FileName = "topArticle_module"; //模块文件名
        $t->Source = "plugin_topArticle"; //模块属性
        $t->SidebarID = 0;
        $t->Content = "";
        $t->HtmlID = "topArticl_module"; //htmlID
        $t->Type = "div"; //模块类型
        $t->Content = acmeContract_topArticle(4); //模块内容
        $t->Save();
    }
    // 主题配置
    Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu', 'acmeContract_AddMenu');
}

function acmeContract_ViewSearch_Begin()
{
    global $zbp;
    $zbp->option['ZC_SEARCH_TYPE'] = 'list';
}

// 时间格式化
function acmeContract_TimeAgo($ptime)
{
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if ($etime < 1) {
        return '刚刚';
    }
    $interval = [
        12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
        30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
        7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
        24 * 60 * 60 => '天前',
        60 * 60 => '小时前',
        60 => '分钟前',
        1 => '秒前',
    ];
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);

            return $r.$str;
        }
    }
}

// 文章列表缩略图
function acmeContract_thumbnail($Source)
{
    global $zbp;
    $temp = mt_rand(1, 20);
    $thumbSrc = '';
    if (ZC_VERSION_COMMIT >= 2800) {
        if ($Source->ImageCount >= 1 && (count($thumbs = $Source->Thumbs(130, 100, 1)) > 0)) {
            $thumb = $thumbs[0];
        } else {
            $thumb = $zbp->host.'zb_users/theme/'.$zbp->theme.'/style/random/'.$temp.'.jpg';
        }
    } else {
        $pattern = "/<img[^>]*src=\"([^\"]+\.(gif|jpg|png))\"[^>]*>/";
        $content = $Source->Content;
        preg_match_all($pattern, $content, $matchContent);
        if (isset($matchContent[1][0])) {
            $thumb = $matchContent[1][0];
        } else {
            $thumb = $zbp->host.'zb_users/theme/'.$zbp->theme.'/style/random/'.$temp.'.jpg';
        }
    }
    $thumbSrc = $thumb;

    return $thumbSrc;
}

// 自定义热门文章
function acmeContract_topArticle($num)
{
    global $zbp,$hot;
    $hot .= '<ul>';
    $array = $zbp->GetArticleList(array('*'), array(array('=','log_Status','0')), array('log_ViewNums'=>'DESC'), array($num), '');
    foreach ($array as $hostArticle) {
        $hot .= '<li><a href="'.$hostArticle->Url.'"><img src="'.acmeContract_thumbnail($hostArticle).'" alt=""><p>'.$hostArticle->Title.'</p></a></li>';
    }
    $hot .= '</ul>';
    
    return $hot;
}


function acmeContract_AddMenu(&$m)
{
    global $zbp;
    $m[] = MakeTopMenu('root', '优杰主题设置', $zbp->host.'zb_users/theme/acmeContract/main.php?act=base', '', 'topmenu_tpure');
}

function InstallPlugin_acmeContract()
{
    global $zbp;
    if (!$zbp->Config('acmeContract')->HasKey('Version')) {
        $array = array(
            'Version' => '1.0',
            'isLogo'  => '0',
            'isQQ'    => '0',
            'QQNUM'   => '12345678',
            'isWX'    => '0',
            'WXNUM'   => '12345678',
            'isHzconv'=> '0'
        );
        foreach ($array as $value => $intro) {
            $zbp->Config('acmeContract')->$value = $intro;
        }
        $zbp->Config('acmeContract')->Version = '1.0';
        $zbp->SaveConfig('acmeContract');
    }
}

function UninstallPlugin_acmeContract()
{
    global $zbp;
    $zbp->DelConfig('acmeContract');
}
